package com.wwz.service;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.deepoove.poi.XWPFTemplate;
import com.wwz.mapper.RentMapper;
import com.wwz.pojo.House;
import com.wwz.pojo.Rent;
import com.wwz.pojo.query.RentQuery;
import com.wwz.util.EcoBootException;
import com.wwz.util.JobUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.IOException;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 租赁接口实现
 * @date 2022/03/26 12:04:59
 */
@Service
public class RentServiceImpl extends ServiceImpl<RentMapper, Rent> implements IRentService {

    @Resource
    private IHouseService houseService;


    @Override
    public IPage<Rent> toPage(RentQuery query) {

        LambdaQueryWrapper<Rent> wrapper = new LambdaQueryWrapper<>();

        if (StrUtil.isNotEmpty(query.getRentUser())){
            wrapper.like(Rent::getRentUser,query.getRentUser());
        }
        wrapper.orderByAsc(Rent::getCreateTime);

        IPage<Rent> page = this.page(query.toPage(), wrapper);

        List<House> houses = houseService.list();
        Map<String, String> map = new HashMap<>(16);
        if (CollUtil.isNotEmpty(houses)){
            map = houses.stream().collect(Collectors.toMap(House::getId,House::getHouseAddress));
        }
        for (Rent rent : page.getRecords()) {
            rent.setRentInfo(map.get(rent.getRentInfo()));
        }

        return page;
    }

    @Override
    public void addOrUpdate(Rent rent) {
        Rent byId = getById(rent);
        if (byId== null){
            rent.setId(StrUtil.uuid());
            rent.setCreateBy(JobUtils.getLogin());
            rent.setCreateTime(new Date());
            save(rent);
            return;
        }
        updateById(rent);
    }

    @Override
    public void export(HttpServletResponse response,String id) {

        Rent rent = this.getById(id);
        if (rent == null){
            throw new EcoBootException("数据不存在");
        }

        HashMap<String, String> map = new HashMap<>();
        map.put("rentUser", rent.getRentUser());
        map.put("houseMaster", rent.getHouseMaster());
        map.put("pay", rent.getPay());
        map.put("rentInfo", rent.getRentInfo());
        map.put("rentStart", rent.getRentStart().toString());
        map.put("rentEnd", rent.getRentEnd().toString());

        String dirPath = JobUtils.getDirPath();
        XWPFTemplate render = XWPFTemplate.compile(dirPath+ File.separator+"rent.docx").render(map);

        BufferedOutputStream stream;
        try {
            response.setContentType("application/force-download");
            response.setHeader("Content-disposition","attachment;filename=\""+"rentInfo.docx"+"\"");

            stream = new BufferedOutputStream(response.getOutputStream());
            render.write(stream);
            stream.close();
            render.close();
        } catch (IOException e) {
            e.printStackTrace();
        }

    }
}
